Next | Prev | Up | Top | Contents | Index

Guidelines for Writing Code for 64-Bit Silicon Graphics Platforms

The key to revising existing code and writing new code that is compatible with all of the major C data models is to avoid the assumptions described above in "Coding Assumptions to Avoid." Since all of the assumptions described sometimes represent legitimate attributes of data objects, you need to tailor declarations to the target machines' data models.

The following guidelines help you to produce portable code. Use these guidelines when you are developing new code or as you identify portability problems in existing code.

  1. In a header file that you include in each of the program's source files, define (typedef) a type for each of the following functions:

  2. Be sure to specify constants with an appropriate type specifier so that they will have the size required by the context with the values expected. Bit masks can be particularly troublesome in this regard: avoid using constants for negative values. For example, 0xffffffff may be equivalent to a -1 on 32-bit systems, but may be interpreted as 4,294,967,295 (signed or unsigned, depending on the mode and context) on most 64-bit systems. The inttypes.h header file provides cpp macros to facilitate this conversion. Defining constants that are sensitive to type sizes in a central header file may help in modifying them when a new port is done.

  3. Where printf/scanf are used for objects whose types are typedefed differently among the targets you must support, you may need to define constant format strings for each of the types defined in step 1. For example:

    #define _fmt32 "%d"

    #define _fmt32u "%u"

    #define _fmt64 "%lld"

    #define _fmt64u "%llu"

    The inttypes.h header file also defines printf/scanf format extensions to standardize these practices. They are implemented by the first release of the 64-bit compilers and related tools.

This chapter provides guidelines for porting code.



Next | Prev | Up | Top | Contents | Index